<?php
namespace app\common\tools;

use think\Config;

/**
 * 分页工具类
 * by @sinalma 2018.07.22
 * 使用:初始化实例对象
*/
class Pagintor{
    
    // 数据表中总记录数
	private $total;
	// 每页显示的行数
	private $listRows;
	// 用于数据库查询
	private $limit;
	// 获取调用此类的文件路径
	private $uri;
	// 总页数
	private $pageNum;
	// 操作列表总数
	private $listNum = 7;
    // 初始操作列表数,1,2,3,4,如果小于4,则显示全部页码
    private $startListNum = 10;
    private $htpURL = '';

    /**
     * 初始化方法
     * $total : 数据表的总记录数
     * $listRows : 每页显示的行数,默认是10
     * $pa :  
     */
    public function __construct($total,$listRows=10,$pa=""){
    	$this->total = $total;
    	$this->listRows = $listRows;
    	$this->uri = $this->getUri($pa);
    	// $this->page = !empty($_GET["page"]) ? $_GET["page"] : 1;
        $pn = input('param.page');
        $this->page = !empty($pn)?$pn:1;
        // echo "(page=".$this->page.")";
    	$this->pageNum = ceil($this->total/$this->listRows);
    	$this->limit = $this->setLimit();
        $this->htpURL = Config::get('HTP_URL');
    }

    // 初始化数据库查询获取的数据限制
    private function setLimit(){
    	return "Limit ".($this->page-1) *$this->listRows.",{$this->listRows}";
    }

    
    /**
     * 获取请求者的路径(不带参数)
     */
    private function getUri($pa){
    	// 如果路径中没有带参数,拼接初始化时传入的参数
    	$url = $_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],'?')?'':"?").$pa;
    	// $url = /framework/data.php?&page=2
    	$parse = parse_url($url);
    	/*
    	* Array
         (
         [path] => /framework/data.php
         [query] => &page=1
          )
    	*/
    	if (isset($parse["query"])) {
    		parse_str($parse['query'],$params);
            /**
            Array
            (
              [page] => 1
            )
            */
    		unset($params["page"]);// $params 为空数组
    		$url = $parse['path'].'?'.http_build_query($params);
    	} 

    	return $url;
    }

    // 魔术方法,返回limit的值
    function __get($args){
    	if ($args == "limit")
    		return $this->limit;
        else
        	return null;
    }

    // 获取当前页第一条数据在总数据中的条数
    private function start() {
    	if ($this->total==0)
    		return 0;
    	else 
    		return ($this->page-1)*$this->listRows+1;
    }

    
    private function end() {
    	return min($this->page*$this->listRows,$this->total);
    }

    private function first(){
    	$html = "";
    	if ($this->page == 1)
    		$html .= '';
    	else 
    		// $html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=1'>{$this->config['first']}</a>";
    		$html .= "&nbsp;&nbsp;<a href='javascript:showpage(\"{$this->uri}&page=1\")'>{$this->config["first"]}</a>&nbsp;&nbsp;";
    	return $html;
    }

    private function prev(){
    	$html = "";
    	if ($this->page == 1){
    		$html .= '';
    	}
    	else {
    		$prevNum = $this->page-1;
    		// $html .= "&nbsp;&nbsp;<a href='{$this->uri}&page=$prevNum'>{$this->config['prev']}</a>&nbsp;&nbsp;";
            $html .= "&nbsp;&nbsp;<a href='javascript:showpage(\"{$this->uri}&page=".($this->page-1)."\")'>{$this->config["prev"]}</a>&nbsp;&nbsp;";
    	}
    	return $html;
    }

    private function pageList(){
    	$linkPage = "";

    	$inum = floor($this->listNum/2);

    	for ($i=$inum;$i>1;$i--){
    		$page = $this->page-$i;

    		if ($page < 1)
    			continue;
            // $linkPage.="&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";
    		$linkPage.="&nbsp;<a href='javascript:showpage(\"{$this->uri}&page={$page}\")'>{$page}</a>&nbsp;";
    	}

    	$linkPage .= "&nbsp;{$this->page}&nbsp;";

    	for($i=1;$i<$inum;$i++){
    		$page = $this->page+$i;
    		if ($page<=$this->pageNum){

				// $linkPage.="&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;";
    			$linkPage.="&nbsp;<a href='javascript:showpage(\"{$this->uri}&page={$page}\")'>{$page}</a>&nbsp;";
    		}else{
    			break;
    		}

    	}
    	return $linkPage;
    }

    private function next(){
        $html = "";
        if ($this->page == $this->pageNum)
        	$html .= '';
        else{
        	$nextPage = $this->page+1;
        	// $html .= "&nbsp;&nbsp;<a href='{$this->uri}&page={$nextPage}'>{$this->config['next']}</a>&nbsp;&nbsp;"; 
        	$html .= "&nbsp;&nbsp;<a href='javascript:showpage(\"{$this->uri}&page=".($this->page+1)."\")'>{$this->config["next"]}</a>&nbsp;&nbsp;";
        }
        return $html;
    }

    private function last(){
    	$html = "";
    	if ($this->page==$this->pageNum)
    		$html .= '';
    	else

        	// $html .= "&nbsp;&nbsp;<a href='{$this->uri}&page={$this->pageNum}'>{$this->config['last']}</a>&nbsp;&nbsp;";
    		$html .= "&nbsp;&nbsp;<a href='javascript:showpage(\"{$this->uri}&page=".($this->pageNum)."\")'>{$this->config["last"]}</a>&nbsp;&nbsp;";
    	return $html;
    }

    private function goPage(){
			return '&nbsp;&nbsp;<input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;showpage(\''.$this->uri.'&page=\'+page+\'\')}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;showpage(\''.$this->uri.'&page=\'+page+\'\')">&nbsp;&nbsp;';
			// return '&nbsp;&nbsp;<input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'">&nbsp;&nbsp;';
    }

    public function fMsgPage(){
        $fpage = '';

        $prevN = $this->page <= 1 ? 1 : $this->page-1;

        $fpage .= "<li><a href='javascript:showmsgpage(\"{$this->htpURL}/public/index.php/index/Message/getMessages/page/$prevN\")'  aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>";
        // 获取该显示的页码数
        $num = $this->pageNum > $this->startListNum ? $this->startListNum : $this->pageNum;

        for($i=1;$i<$num+1;++$i){
            $fpage .= "<li><a href='javascript:showmsgpage(\"{$this->htpURL}/public/index.php/index/Message/getMessages/page/$i\")'>$i</a></li>";
        }

        // $fpage .= $this->pageNum > $this->startListNum ? "<li>...</li>":"";
        $nextN = $this->page >= $this->pageNum ? $this->pageNum : $this->page+1;
$fpage  .= <<<eof
             <li><a id="msg-bot-next" href='javascript:showmsgpage("{$this->htpURL}/public/index.php/index/Message/getMessages/page/$nextN")' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>
eof;


$html = "";
$html = <<<eof
           <nav aria-label="Page navigation">
           <ul class="pagination">
           {$fpage}
           </ul>
           </nav>
eof;
        return $html;
    }

    /**
     * 全数字页码分页(文章)
     */
    public function fnumpage(){

        $fpage = '';

        $prevN = $this->page <= 1 ? 1 : $this->page-1;

        $fpage .= "<li><a href='javascript:showpage(\"{$this->htpURL}/public/index.php/index/Index/getArticles/page/$prevN\")'  aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>";
        // 获取该显示的页码数
        $num = $this->pageNum > $this->startListNum ? $this->startListNum : $this->pageNum;

        for($i=1;$i<$num+1;++$i){
            $fpage .= "<li><a href='javascript:showpage(\"$this->htpURL/public/index.php/index/index/getArticles/page/$i\")'>$i</a></li>";
        }

        $fpage .= $this->pageNum > $this->startListNum ? "<li>...</li>":"";
        $nextN = $this->page >= $this->pageNum ? $this->pageNum : $this->page+1;
$fpage  .= <<<eof
             <li><a href='javascript:showpage("$this->htpURL/public/index.php/index/index/getArticles/page/$nextN")' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>
eof;


$html = "";
$html = <<<eof
           <nav aria-label="Page navigation" class="naviPage">
           <ul class="pagination">
           {$fpage}
           </ul>
           </nav>
eof;
        return $html;
    }

    


}